<?php
/**
 * 兑换模型
 *
 */
namespace Admin\Model;
use Think\Model;
class PExchangeModel extends BaseModel{

	private $giftType = array(1=>'话费/流量', 2=>'影视类', 3=>'电子券', 4=>'特殊活动');
	private $isYesOrNo = array(0=>'否', 1=>'是');

	public function getList($data)
	{
		$map['is_deleted'] = 0;
		if (isset($data['name']) and $data['name'] != '') {
			$map['name'] = array('LIKE', '%'.$data['name'].'%');
		}
		if (isset($data['type']) and $data['type'] != 0) {
			$map['type'] = $data['type'];
		}
		$total = $this->where($map)->count();
		$page = pagination($total, $data['page_size'] ,$data['page']);
		if ($total) {
			$list = $this->where($map)->order('top desc,id desc')
			      ->limit($page['offset'], $page['limit'])
			      ->select();
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
				$list[$k]['type'] = $this->giftType[$v['type']];
				$list[$k]['is_show'] = $this->isYesOrNo[$v['is_show']];
                $list[$k]['material'] = $this->isYesOrNo[$v['material']];
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);
	}


	/**
	 * 获取积分记录
	 * @param  array  $data 过滤参数
	 * @return  
	 */
	public function getRecordList($data = array(), $down = false)
	{
		$model = M('PScoreRecord');
		$model->alias('r')->join('__P_USER__ u ON u.id = r.user_id', 'LEFT')
		      ->join('__P_EXCHANGE__ e ON r.act_id = e.id', 'LEFT');
		//组装查询条件
		$where['r.is_deleted'] = 0;
		$where['r.type'] = 3;

		if(isset($data['keyword']) and $data['keyword'] != ''){
			$or['u.name'] = array('LIKE', '%'.$data['keyword'].'%');
			$or['u.phone'] = array('LIKE', '%'.$data['keyword'].'%');
			$or['u.account'] = array('LIKE', '%'.$data['keyword'].'%');
			$or['_logic'] = 'or';
			$where['_complex'] = $or;
		}
		//活动名称
		if (isset($data['name']) and $data['name'] != '') {
			$where['e.name'] = array('LIKE', '%'.$data['name'].'%');
		}
		
		
		//活动创建时间开始区间
		if (isset($data['start']) and $data['start']) {
			$where['r.add_time'][] = array('egt', strtotime($data['start']));
		}
		//活动创建时间结束区间
		if (isset($data['end']) and $data['end']) {
			$where['r.add_time'][] = array('elt', strtotime($data['end'].' 23:59:59'));
		}
		
		$total = $model->where($where)->count();
		$pageIndex = I('post.page') ?: 1;
		$pageSize = I('post.page_size') ?: 20;
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			if($down){
				$list = $model->alias('r')
						->field('r.*,u.name as username,u.account,u.phone,u.address,e.name')
						->join('__P_USER__ u ON u.id = r.user_id', 'LEFT')
						->join('__P_EXCHANGE__ e ON r.act_id = e.id', 'LEFT')
			      		->where($where)->order('r.id desc')->select();
			}else{
				$list = $model->alias('r')
						->field('r.*,u.name as username,u.account,u.phone,u.address,e.name')
						->join('__P_USER__ u ON u.id = r.user_id', 'LEFT')
						->join('__P_EXCHANGE__ e ON r.act_id = e.id', 'LEFT')
				      	->where($where)->order('r.id desc')
				      	->limit($page['offset'], $page['limit'])->select();
			  }
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
				$list[$k]['score'] = abs($v['score']);
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}


}
?>